package com.example.lss.news.fragment;


import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.lss.news.R;
import com.example.lss.news.adapter.ReAdapter;
import com.example.lss.news.bean.Data;
import com.example.lss.news.bean.Ress;
import com.example.lss.news.bean.Result;
import com.example.lss.news.utils.JsonUtils;
import com.example.lss.news.utils.app.BaseFrament;
import com.example.lss.news.utils.http.HttpUtils;
import com.example.lss.news.utils.http.JsonCallback;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;


public class TopFragmet extends BaseFrament {
    private RecyclerView rv;
    ReAdapter reAdapter;
    private Ress<Result<List<Data>>> r = null;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<Data> list;
    private TwinklingRefreshLayout refreshLayout;
    private Handler handler = new Handler();

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.frag_view, container, false);
        rv = view.findViewById(R.id.re);
//        rv.addItemDecoration(new DividerItemDecoration(act, DividerItemDecoration.VERTICAL));
//        swipeRefreshLayout = view.findViewById(R.id.gank_swipe_refresh_layout);
        refreshLayout = view.findViewById(R.id.refreshLayout);

        return view;
    }

    @Override
    protected void initData() {

        new Thread() {


            @Override
            public void run() {
                HttpUtils.get("http://v.juhe.cn/toutiao/index?type=top&key=9980cb57a9269e256bcc015d498dd572", new JsonCallback() {


                    @Override
                    public void onStart(Object tag) {

                    }

                    @Override
                    public void onSuccess(final String json) {


                        r = JsonUtils.toBean(json, JsonUtils.newParamType(Ress.class, JsonUtils.newParamType(Result.class, JsonUtils.newParamType(List.class, Data.class))));

                        if (r.getReason().equals("成功的返回")) {
                            list = new ArrayList<>();
                            list.addAll(r.getResult().getData());
                            reAdapter = new ReAdapter(list);
                            rv.setLayoutManager(new LinearLayoutManager(act));
                            rv.setAdapter(reAdapter);
                            refreshLayout.finishRefreshing();
                            refreshLayout.finishLoadmore();

//                            swipeRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onError(Throwable exception) {

                    }

                    @Override
                    public void onFinish() {

                    }
                });
            }
        }.start();
    }

    @Override
    protected void initListener() {
//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                initData();
//                list.addAll(r.getResult().getData());
//                reAdapter = new ReAdapter(list);
//                rv.setLayoutManager(new LinearLayoutManager(act));
//                rv.setAdapter(reAdapter);
//                reAdapter.notifyDataSetChanged();
//            }
//        });
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                        list.addAll(r.getResult().getData());
                        reAdapter = new ReAdapter(list);
                        rv.setLayoutManager(new LinearLayoutManager(act));
                        rv.setAdapter(reAdapter);
                        reAdapter.notifyDataSetChanged();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                        list.addAll(r.getResult().getData());
                        reAdapter = new ReAdapter(list);
                        rv.setLayoutManager(new LinearLayoutManager(act));
                        rv.setAdapter(reAdapter);
                        reAdapter.notifyDataSetChanged();
                    }
                }, 2000);
            }
        });

    }
}
